package com.blb.community_admin.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.blb.community_admin.Annotation.Log;
import com.blb.community_admin.entity.ZyCommunity;
import com.blb.community_admin.entity.ZyUnit;
import com.blb.community_admin.service.IZyUnitService;
import com.blb.community_admin.util.EasyExcelUtil;
import com.blb.community_admin.util.ResponseResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.core.io.ByteArrayResource;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.io.IOException;
import java.util.Arrays;
import java.util.List;

/**
 * 单元控制器
 */
@Api(tags = "房屋模块")
@RestController
@RequestMapping("/zyUnit")
@CrossOrigin
public class ZyUnitController {

    @Resource
    private IZyUnitService unitService;

    @GetMapping("/{buildingName}")
    public ResponseResult<List<ZyUnit>> selectZyUnitByBuildingId(@PathVariable String buildingName) {
        List<ZyUnit> zyUnits = unitService.selectUnit(buildingName);
        return ResponseResult.ok(zyUnits);
    }

//    @GetMapping("/")
//    public ResponseResult<IPage<ZyUnit>> selectUnitPage(Long current ,Long size) {
//        System.out.println(current);
//        IPage<ZyUnit> zyUnitIPage = unitService.selectUnitPage(current, size);
//        return ResponseResult.ok(zyUnitIPage);
//    }
    //模糊查询分页

    @GetMapping("/page/fuzzy")
    public ResponseResult<IPage<ZyUnit>> fuzzyPage(Long current, Long size, String unitName, String unitCode, String buildingName) {
        IPage<ZyUnit> page = unitService.fuzzyPage(current, size, unitName, unitCode, buildingName);
        return ResponseResult.ok(page);
    }

    @PostMapping("")
    public ResponseResult<String> addzyunit(@RequestBody ZyUnit zyUnit) {
        unitService.save(zyUnit);
        return ResponseResult.ok("添加成功");
    }

    @PutMapping("")
    public ResponseResult<String> updatazyunit(@RequestBody ZyUnit zyUnit) {

        unitService.updateById(zyUnit);
        return ResponseResult.ok("修改成功");
    }

    @PostMapping("/delete")
    public ResponseResult<String> deletezyunit(@RequestBody Long[] arrs) {
            List<Long> list = Arrays.asList(arrs);
            unitService.removeByIds(list);
            return ResponseResult.ok("删除成功");
    }

    @Log(module = "单元", operationType = 5, requestMethod = "POST")
    @ApiOperation("导出")
    @PostMapping("/export")
    public ResponseEntity<ByteArrayResource> export(@RequestBody List<ZyUnit> zyUnits) throws IOException {
        String path = EasyExcelUtil.write("单元数据", ZyUnit.class, zyUnits);
        ResponseEntity<ByteArrayResource> download = EasyExcelUtil.download(path);
        return download;
    }
}
